/*****************************************************************************
*
* File: hw_init.c
*
* Description: Contains functions to initialized hardware on MCU.
*
* Author: Kevin Casteel, Luis Castillo
*
* Revisions: v-1.0 (4/18/11)
*
******************************************************************************/


#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "hw_init.h"


/************************************************ 
 *
 * Function: vInit_HW()
 *
 * Description: Initializes ports.
 *
 ************************************************/
void vInit_HW()
{
	//- front two layers
	DDRA = ALL;
	//- back two layers
	DDRC = ALL;
	//- horizontal layers
	DDRD = MS6;

	PORTA = NONE;
	PORTC = NONE;
	PORTD = NONE;
}


/************************************************ 
 *
 * Function: vInit_Test()
 *
 * Description: Tests every LED.
 *
 ************************************************/
void vInit_Test()
{
	volatile uint8_t i, j;

	//- animation that loops through each
	//- LED for initial testing.
	for(i = 2; i < 8; i++)
	{
		PORTD = (1 << i);
	
		for(j = 0; j < 8; j++)
		{
			PORTA = (1 << j);
			_delay_ms(50);
		}
		PORTA = 0x00;

		for(j = 0; j < 8; j++)
		{
			PORTC = (1 << j);
			_delay_ms(50);
		}
		PORTC = 0x00;
	}
	for(i = 2; i < 8; i++)
	{
		PORTD = (1 << i);
	
		for(j = 0; j < 8; j++)
		{
			PORTA = ALL;
			PORTC = ALL;
			_delay_ms(50);
		}
	}
	PORTA = NONE;
	PORTC = NONE;
	PORTD = NONE;
}


/************************************************ 
 *
 * Function: USART_Init()
 *
 * Description: Initializes USART port.
 *
 ************************************************/
void vUSART_Init()
{
	/* Set baud rate */
	UBRRH = (unsigned char)(BAUD>>8);
	UBRRL = (unsigned char)BAUD;
	
	/* Enable receiver and receiver interrupt */
	UCSRB = (1<<RXEN)|(1<<RXCIE);

	/* Set frame format: 8data, 1stop bit */
	UCSRC = (1<<URSEL)|(1<<UMSEL)|(3<<UCSZ0);
}


/************************************************ 
 *
 * Function: USART_Receive()
 *
 * Description: Receives data from USART.
 *
 * Return: Received data from buffer.
 *
 ************************************************/
uint8_t xUSART_Receive()
{
	/* Wait for data to be received */
	while ( !(UCSRA & (1<<RXC)) )
		;
	
	/* Get and return received data from buffer */
	return UDR;
}


/************************************************ 
 *
 * Function: vLeds()
 *
 * Description: Display value to top layer of
 *              LEDs.
 *
 * Parameters: data - value to be displayed
 *
 ************************************************/
void vLeds(uint8_t data)
{
	//- enable top layer
	PORTD = MSB;

	//- write data to front two rows
	PORTA = data;
}


/************************************************ 
 *
 * Function: vClearCube()
 *
 * Description: Clears contents of cube.
 *
 * Parameters: cube - Pointer to cube.
 *
 ************************************************/
void vClearCube(cube_t *cube)
{
	volatile int i, j, k;

	//- loop through each element in structure
	//- and initialize it to zero
	for(i=0;i<CUBEHEIGHT;i++)
	{
		for(j=0;j<CUBELENGTH;j++)
		{
			for(k=0;k<CUBEWIDTH;k++)
			{
				cube->layers[i].leds[j][k] = 0;
			}
		}
	}
}


/************************************************ 
 *
 * Function: vInitSnake()
 *
 * Description: Initializes length of snake.
 *
 * Parameters: location - initial location of
 *                        snake's head.
 *
 ************************************************/
void vInitSnake(location_t *snake)
{
	volatile int i;

	//- initialize head location
	snake[0].layer = 2;
	snake[0].row   = 3;
	snake[0].col   = 3;

	//- since snake's initial length is 1,
	//- other parts are cleared.
	for(i=1;i<MAXSNAKELENGTH;i++)
	{
		snake[i].layer = -1;
		snake[i].row   = -1;
		snake[i].col   = -1;
	}
}
